package pers.qianyu.module.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
import pers.qianyu.module.core.domain.system.converter.SysUserFollowConverter;
import pers.qianyu.module.core.domain.system.dto.SysUserFollowDTO;
import pers.qianyu.module.core.domain.system.entity.SysUserFollowPO;
import pers.qianyu.module.system.dao.SysUserFollowDao;
import pers.qianyu.module.system.listener.events.UserFollowedUserIdsUpdatedEvent;
import pers.qianyu.module.system.service.SysUserFollowService;

import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author mizzle rain
 * @date 2021-04-07 15:16
 */
@Service("sysUserFollowService")
public class SysUserFollowServiceImpl implements SysUserFollowService {
    @Autowired
    private SysUserFollowDao sysUserFollowDao;
    @Autowired
    private SysUserFollowConverter sysUserFollowConverter;

    @Autowired
    private ApplicationContext applicationContext;

    @Override
    public void insert(SysUserFollowDTO sysUserFollowDTO) {
        QueryWrapper<SysUserFollowPO> wrapper = new QueryWrapper<>();
        Long userId = sysUserFollowDTO.getUserId();
        wrapper.eq("user_id", userId);
        wrapper.eq("followed_user_id", sysUserFollowDTO.getFollowedUserId());
        SysUserFollowPO sysUserFollowPOFromDB = sysUserFollowDao.selectOne(wrapper);
        if (Objects.nonNull(sysUserFollowPOFromDB)) {
            // 已经关注的就不能再关注了
            return;
        }
        SysUserFollowPO sysUserFollowPO = sysUserFollowConverter.convertToPO(sysUserFollowDTO);
        sysUserFollowDao.insert(sysUserFollowPO);
        // 更新Redis 中信息
        Set<Long> followedUserIds = getMyFollow(userId);
        applicationContext.publishEvent(new UserFollowedUserIdsUpdatedEvent(this, followedUserIds, userId));
    }

    @Override
    public Set<Long> getFans(Long userId) {
        QueryWrapper<SysUserFollowPO> wrapper = new QueryWrapper<>();
        wrapper.eq("followed_user_id", userId);
        List<SysUserFollowPO> sysUserFollowPOS = sysUserFollowDao.selectList(wrapper);
        return sysUserFollowPOS.stream()
                .map(SysUserFollowPO::getUserId)
                .collect(Collectors.toSet());
    }

    @Override
    public Set<Long> getMyFollow(Long userId) {
        QueryWrapper<SysUserFollowPO> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        List<SysUserFollowPO> sysUserFollowPOS = sysUserFollowDao.selectList(wrapper);
        return sysUserFollowPOS.stream()
                .map(SysUserFollowPO::getFollowedUserId)
                .collect(Collectors.toSet());
    }

    @Override
    public void delete(SysUserFollowDTO sysUserFollowDTO) {
        QueryWrapper<SysUserFollowPO> wrapper = new QueryWrapper<>();
        Long userId = sysUserFollowDTO.getUserId();
        wrapper.eq("user_id", userId);
        wrapper.eq("followed_user_id", sysUserFollowDTO.getFollowedUserId());
        sysUserFollowDao.delete(wrapper);
        // 更新Redis 中信息
        Set<Long> followedUserIds = getMyFollow(userId);
        applicationContext.publishEvent(new UserFollowedUserIdsUpdatedEvent(this, followedUserIds, userId));
    }
}
